package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.salesmanage.util.RedempOrderDetail;
import com.turing.post.salesmanage.util.RedempOrderDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 赎回单明细
 * 
 * @author zxp
 * @Jan 29, 2013
 */
@Stateless(mappedName = "salesmanage.impl.RedempOrderDetailDaoImpl")
@Remote
@Local
public class RedempOrderDetailDaoImpl implements RedempOrderDetailDao {

	@PersistenceContext
	protected EntityManager _em;

	@Override
	public RedempOrderDetail getRedempOrderDetailByRedempOrderDetail(
			RedempOrderDetail rod) {
		return _em.find(RedempOrderDetail.class, rod);
	}

	@SuppressWarnings("unchecked")
	@Override
	public RedempOrderDetail getRedempOrderDetailForUpdate(RedempOrderDetail rod) {
		if (rod.getRedempOrderId() == null || rod.getMasterAccNo() == null) {
			return null;
		}
		String sql = "select t.* from RedempOrderDetail t where t.redempOrderId = ?1 and masterAccNo=?2 for update";
		Query query = _em.createNativeQuery(sql, RedempOrderDetail.class);
		query.setParameter(1, rod.getRedempOrderId());
		query.setParameter(1, rod.getMasterAccNo());
		List<RedempOrderDetail> macc = query.getResultList();
		if (macc == null || macc.size() != 1)
			return null;
		return macc.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RedempOrderDetail> getRedempOrderDetailListByRedempOrderId(
			BigInteger redempOrderId) {
		if (redempOrderId == null)
			return null;
		String sql = "select t.* from RedempOrderDetail t where t.redempOrderId = ?1 order by masterAccNo";
		Query query = _em.createNativeQuery(sql, RedempOrderDetail.class);
		query.setParameter(1, redempOrderId);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RedempOrderDetail> getRedempOrderDetailByCondition(
			BigInteger redempOrderId, Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from RedempOrderDetail where redempOrderId ="
				+ redempOrderId + " order by masterAccNo";
		Query query = _em.createNativeQuery(sql, RedempOrderDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getRedempOrderDetailTotalByCondition(
			BigInteger redempOrderId) {
		String sql = "select nvl(count(*),0) from RedempOrderDetail where redempOrderId ="
				+ redempOrderId;
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertRedempOrderDetail(RedempOrderDetail rod)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, rod);
	}

	@Override
	public void deleteRedempOrderDetail(RedempOrderDetail rod)
			throws PCPSApplicationException {
		RedempOrderDetail del = getRedempOrderDetailByRedempOrderDetail(rod);
		if (del != null) {
			GeneralDao.remove(_em, del);
		}
	}
}